﻿<Window x:Class="_11.DataBinding.InvalidErrorTips"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:_11.DataBinding"
        Title="InvalidErrorTips" Height="120" Width="300">
    <Window.Resources>
        <local:Person x:Key="someone" Name="cheka" Age="18"/>
    </Window.Resources>
    <Grid DataContext="{StaticResource someone}">
        <Grid.Resources>
            <Style TargetType="Label">
                <Setter Property="Margin" Value="5"/>
                <Setter Property="FontWeight" Value="ExtraBlack"/>
                <Setter Property="HorizontalContentAlignment" Value="Right"/>
                <Setter Property="VerticalContentAlignment" Value="Center"/>
            </Style>
            <Style TargetType="TextBox">
                <Setter Property="Margin" Value="5"/>
                <Setter Property="HorizontalContentAlignment" Value="Center"/>
                <Setter Property="VerticalContentAlignment" Value="Center"/>
                <Setter Property="Background" Value="Silver"/>
                <Setter Property="BorderThickness" Value="1"/>
                <Setter Property="BorderBrush" Value="black"/>
            </Style>
        </Grid.Resources>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="3*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Label Grid.Row="0" Grid.Column="0" Content="Name: "  />
        <Label Grid.Row="1" Grid.Column="0" Content="Age: "  />
        <TextBox Grid.Row="0" Grid.Column="1"  Text="{Binding Path=Name}" />
        <!--note: bind the tooltip of this control to itself's attached property-->
        <TextBox Grid.Row="1" Grid.Column="1"  
                 ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}">
            <!--below syntax can achieve the same effect as the above one
            but maybe not an elegant way to code
        <TextBox Grid.Row="1" Grid.Column="1"  
                 Name="tbxAge"
                 ToolTip="{Binding ElementName=tbxAge, Path=(Validation.Errors)[0].ErrorContent}">
         -->
            <TextBox.Text>
                <Binding Path="Age" >
                    <Binding.ValidationRules>
                        <local:AgeValidator Min="0" Max="100"/>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>
    </Grid>
</Window>
